/**
 * 
 */
package com.firebird.oms.web.dwr.sm;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;

import com.firebird.base.domain.Page;
import com.firebird.oms.constants.AuthConstants;
import com.firebird.oms.constants.BusiConstants;
import com.firebird.oms.domain.sm.TismArea;
import com.firebird.oms.domain.sm.TismCompany;
import com.firebird.oms.domain.sm.TismStaff;
import com.firebird.oms.domain.sm.TismStaticValue;
import com.firebird.oms.domain.sm.TismWorker;
import com.firebird.oms.exception.OmsRuntimeException;
import com.firebird.oms.service.sm.CompanyService;
import com.firebird.oms.service.sm.StaffService;
import com.firebird.oms.service.sm.StaticService;
import com.firebird.oms.user.UserDetails;
import com.firebird.oms.web.base.BaseDwr;

/**
 * @author qiuxj
 * @date 2009-11-21
 *
 */
public class StaffDwr extends BaseDwr {
	
	private StaffService staffService;
	private StaticService staticService;
	private CompanyService companyService;
	
	/**
	 * 获取员工信息
	 * @param staffId
	 * @return
	 */
	public TismStaff getStaff(int staffId) {
		return staffService.getStaff(staffId);
	}
	
	/**
	 * 更新员工信息
	 * @param smStaff
	 */
	public void updateStaff(TismStaff smStaff) {
		staffService.updateStaff(smStaff);
	}
	
	/**
	 * 创建员工信息
	 * @param smStaff
	 */
	public void createStaff(TismStaff smStaff){
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SYSTEM){
			throw new OmsRuntimeException("权限不足，无法增加员工信息！");
		}
		smStaff.setUpperStaffId(user.getStaffId());
		staffService.createStaff(smStaff);
	}
	
	/**
	 * 创建工号
	 * @param smWorker
	 */
	public void createWorker(TismWorker smWorker){
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SYSTEM){
			throw new OmsRuntimeException("权限不足，无法增加员工信息！");
		}
		
		TismWorker worker = this.getWorkerByLoginCode(smWorker.getLoginCode());
		if(worker != null){
			throw new OmsRuntimeException("工号已存在，请重新填写！");
		}
		
		Map<String, String> filter = new HashMap<String, String>();
		filter.put("staffId", smWorker.getStaffId().toString());
		List<TismWorker> workers = staffService.findWorkers(filter);
		if(workers.size() > 0 && !AuthConstants.CAN_CREATE_MORE_LOGIN_CODE){
			throw new OmsRuntimeException("不允许一个员工创建多个工号！");
		}
		
		smWorker.setLoginNum(0);
		smWorker.setLoginPwd(AuthConstants.DEFAULT_PWD);
		smWorker.setLoginType(1);
		smWorker.setLastChgPwdTime(new Date());
		staffService.createWorker(smWorker);
	}
	
	/**
	 * 更新工号信息
	 * @param smWorker
	 */
	public void updateWorker(TismWorker smWorker){
		staffService.updateWorker(smWorker);
	}
	
	/**
	 * 获取工号信息
	 * @param loginId
	 * @return
	 */
	public TismWorker getWorker(int loginId){
		return staffService.getWorker(loginId);
	}
	
	/**
	 * @param loginId
	 * @return
	 */
	public TismWorker getWorkerAndExt(int loginId) {
		TismWorker worker = this.getWorker(loginId);
		if (worker != null) {
			worker.getRoles().size();
		}

		return worker;
	}
	
	/**
	 * 根据登录账号获取工号信息
	 * @param loginCode
	 * @return
	 */
	public TismWorker getWorkerByLoginCode(String loginCode){
		return staffService.getWorkerByLoginCode(loginCode);
	}
	
	/**
	 * 校验当前员工是否可以添加工号
	 * @param staffId
	 * @return
	 */
	public Map<String, String> validStaff(String staffId) {
		Map<String, String> map = new HashMap<String, String>();
		UserDetails user = super.getCurrentUser();
		if(user.getLoginlevel() > AuthConstants.WORKER_LEVEL_SYSTEM){
			map.put("code", "-1");
			map.put("desc", "权限不足，无法增加员工信息！");
			return map;
		}

		Map<String, String> filter = new HashMap<String, String>();
		filter.put("staffId", staffId);
		List<TismWorker> workers = staffService.findWorkers(filter);
		if(workers.size() > 0 && !AuthConstants.CAN_CREATE_MORE_LOGIN_CODE && user.getLoginlevel() > AuthConstants.WORKER_LEVEL_ADMIN){
			map.put("code", "-1");
			map.put("desc", "不允许一个员工创建多个工号！");
			return map;
		}
		
		map.put("code", "0");
		return map;		
	}
	
	/**
	 * 获取员工信息列表，包括当前登录工号
	 * @param filter
	 * @param likeMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pageStaff(Map<String, String> filterMap, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize) {
		UserDetails user = super.getCurrentUser();
		List<TismStaff> staffs = staffService.getStaffs(user.getStaffId());
		
		for(TismStaff staff : staffs){
			Map<String, String> filter = new HashMap<String, String>();
			filter.put("staticTypeId", BusiConstants.STATIC_TYPE_PLACE_TYPE);
			filter.put("staticValue", staff.getPlaceId().toString());
			List<TismStaticValue> staticValue = staticService.findStaticValue(filter);
			staff.setPlaceName(staticValue != null && staticValue.size() > 0?staticValue.get(0).getStaticDesc():"");
			
			TismArea area = companyService.getArea(staff.getDepartId());
			staff.setDepartName(area != null?area.getAreaName():"");
			
			TismCompany company = companyService.getCompany(staff.getCompanyId());
			staff.setCompanyName(company != null?company.getCompanyName():"");
		}
		return new Page(staffs, pageNo, staffs.size(), pageSize);
	}
	
	/**
	 * 获取员工下的所有工号
	 * @param filter
	 * @param likeMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pageWorker(Map<String, String> filter, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize){
//		UserDetails user = super.getCurrentUser();
//		filter.put("staffId", user.getStaffId().toString());
		List<TismWorker> workers = staffService.findWorkers(filter);
		return new Page(workers, pageNo, workers.size(), pageSize);
	}
	
	/**
	 * 获取当前用户所有员工的所有账号信息
	 * @param filter
	 * @param likeMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page pageStaffAndWorker(Map<String, String> filter, Map<String, String> likeMap, Map<String, String> sortMap,
			int pageNo, int pageSize){
		UserDetails user = super.getCurrentUser();
		List<TismStaff> staffs = staffService.getStaffs(user.getStaffId());
		
		List<Map<String, Object>> results = new ArrayList<Map<String,Object>>(0);
		for(TismStaff staff : staffs){
			Integer staffId = staff.getStaffId();
			
			Map<String, String> workerFilter = new HashMap<String, String>();
			workerFilter.put("staffId", staffId.toString());
			List<TismWorker> workers = staffService.findWorkers(workerFilter);
			
			for(TismWorker worker : workers){
				Map<String, Object> workerMap = new HashMap<String, Object>();
				try {
					workerMap = BeanUtils.describe(worker);
				} catch (Exception e) {
					super.logger.info("Bean转换成Map异常。", e);
				}
				
				workerMap.put("staffName", staff.getStaffName());
				workerMap.put("relaPhone", staff.getRelaPhone());
				workerMap.put("relaMobile", staff.getRelaMobile());
				workerMap.put("relaAddr", staff.getRelaAddr());
				workerMap.put("staffState", staff.getState());
				workerMap.put("companyId", staff.getCompanyId());
				workerMap.put("departId", staff.getDepartId());
				workerMap.put("staffId", staff.getStaffId());
				results.add(workerMap);
			}
		}
		
		return  new Page(results, pageNo, results.size(), pageSize);
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

	public void setStaticService(StaticService staticService) {
		this.staticService = staticService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

}
